import org.bukkit.Bukkit;
import org.bukkit.Location;
import org.bukkit.configuration.file.FileConfiguration;
import org.bukkit.entity.Player;
import org.bukkit.plugin.java.JavaPlugin;
import redis.clients.jedis.Jedis;

import java.util.Iterator;
import java.util.Set;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class RedisInstance {
    private static RedisInstance instance = null;
    private Jedis jedis = null;

    public static RedisInstance getInstance() {
        return instance == null ? instance = new RedisInstance() : instance;
    }

    public void enable() {
        jedis = new Jedis("localhost");
    }

    public void disable() {
        jedis.close();
    }

    public String getApply(String name) {
        try {
            return jedis.get("minecraft_apply:" + name);
        } catch (Exception e) {
            return null;
        }
    }

    public Boolean setApply(String name, String target) {
        try {
            jedis.set("minecraft_apply:" + target, name);
            jedis.expire("minecraft_apply:" + target, 60);
        } catch (Exception e) {
            return false;
        }
        return true;
    }

    public Boolean delApply(String name) {
        try {
            jedis.del("minecraft_apply:" + name);
        } catch (Exception e) {
            return false;
        }
        return true;
    }

    public String getBack(String name) {
        try {
            return jedis.get("minecraft_back:" + name);
        } catch (Exception e) {
            return null;
        }
    }

    public Boolean setBack(String name) {
        try {
            Player player = Bukkit.getPlayer(name);
            Location location = player.getLocation();
            String result = location.getWorld().getName() + "," + location.getX() + "," + location.getY() + "," + location.getZ();
            jedis.set("minecraft_back:" + name, result);
        } catch (Exception e) {
            return false;
        }
        return true;
    }

    public Boolean delBack(String name) {
        try {
            jedis.del("minecraft_back:" + name);
        } catch (Exception e) {
            return false;
        }
        return true;
    }

    public String getHome(String name) {
        try {
            return jedis.get("minecraft_home:" + name);
        } catch (Exception e) {
            return null;
        }
    }

    public Boolean setHome(String name) {
        try {
            Player player = Bukkit.getPlayer(name);
            Location location = player.getLocation();
            String result = location.getWorld().getName() + "," + location.getX() + "," + location.getY() + "," + location.getZ();
            jedis.set("minecraft_home:" + name, result);
        } catch (Exception e) {
            return false;
        }
        return true;
    }

    public Boolean delHome(String name) {
        try {
            jedis.del("minecraft_home:" + name);
        } catch (Exception e) {
            return false;
        }
        return true;
    }

    public String getNick(String name) {
        try {
            return jedis.get("minecraft_nick:" + name);
        } catch (Exception e) {
            return null;
        }
    }

    public Boolean setNick(String name, String str) {
        try {
            {
                String msg = str;
                Pattern p = Pattern.compile("&[0-9a-f]");
                Matcher m = p.matcher(msg);
                while (m.find()) {
                    String target = m.group();
                    String result = target.replace("&", "§");
                    msg = msg.replace(target, result);
                }
                str = msg;
            }
            jedis.set("minecraft_nick:" + name, str);
        } catch (Exception e) {
            return false;
        }
        return true;
    }

    public Boolean delNick(String name) {
        try {
            jedis.del("minecraft_nick:" + name);
        } catch (Exception e) {
            return false;
        }
        return true;
    }

    public String getPrefix(String name) {
        try {
            return jedis.get("minecraft_prefix:" + name);
        } catch (Exception e) {
            return null;
        }
    }

    public Boolean setPrefix(String name, String str) {
        try {
            {
                String msg = str;
                Pattern p = Pattern.compile("&[0-9a-f]");
                Matcher m = p.matcher(msg);
                while (m.find()) {
                    String target = m.group();
                    String result = target.replace("&", "§");
                    msg = msg.replace(target, result);
                }
                str = msg;
            }
            jedis.set("minecraft_prefix:" + name, str);
        } catch (Exception e) {
            return false;
        }
        return true;
    }

    public Boolean delPrefix(String name) {
        try {
            jedis.del("minecraft_prefix:" + name);
        } catch (Exception e) {
            return false;
        }
        return true;
    }

    public String getSuffix(String name) {
        try {
            return jedis.get("minecraft_suffix:" + name);
        } catch (Exception e) {
            return null;
        }
    }

    public Boolean setSuffix(String name, String str) {
        try {
            {
                String msg = str;
                Pattern p = Pattern.compile("&[0-9a-f]");
                Matcher m = p.matcher(msg);
                while (m.find()) {
                    String target = m.group();
                    String result = target.replace("&", "§");
                    msg = msg.replace(target, result);
                }
                str = msg;
            }
            jedis.set("minecraft_suffix:" + name, str);
        } catch (Exception e) {
            return false;
        }
        return true;
    }

    public Boolean delSuffix(String name) {
        try {
            jedis.del("minecraft_suffix:" + name);
        } catch (Exception e) {
            return false;
        }
        return true;
    }

    public String getTeam(String name) {
        try{
            return jedis.get("minecraft_team:"+name);
        }catch(Exception e){
            return null;
        }
    }
}
